<template>
  <div class="booksearch">
    <!-- 搜索栏 -->
    <van-sticky>
      <div class="search_max">
        <div class="search">
          <div class="search_icon">
            <img src="../assets/img/search.png" />
          </div>
          <input type="text" placeholder="请输入图书名称" @change="search" />
        </div>
        <div class="sancel">
          <router-link to="/books">
            <h2>取消</h2>
          </router-link>
        </div>
      </div>
    </van-sticky>
    <!-- 无搜索结果 -->
    <van-empty
      v-show="this.cart==false"
      class="custom-image"
      image="search"
      description="请输入相关的内容~"
    />
    <!-- 有搜索结果 -->
    <div class="search_res" v-for="(item, i) of cart" :key="i">
      <div class="left">
        <img :src="item.pic" />
      </div>
      <div class="right">
        <div class="top">{{ item.title }}</div>
        <div class="botton">
          <div class="botton_left">{{ item.orderNumber }}人购买</div>
          <div class="botton_right">
            <span>￥65.8</span>
            <span>¥{{ item.price }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style lang="scss">
.booksearch {
  .search_max {
    // 搜索栏
    height: 3.4375rem;
    display: flex;
    flex-wrap: wrap;
    align-content: center;
    justify-content: center;
    background-color: #fff;
    .search {
      height: 1.6875rem;
      display: flex;
      input {
        width: 16.25rem;
        border: none;
        background-color: #ccc;
        border-top-right-radius: .3125rem;
        border-bottom-right-radius: .3125rem;
        background-color: #f2f2f2;
        font-size: .0625rem;
      }
      .search_icon {
        padding-left: .3125rem;
        padding-right: .3125rem;
        line-height: 1.5625rem;
        border-top-left-radius: .3125rem;
        border-bottom-left-radius: .3125rem;
        background-color: #f2f2f2;
        img {
          width: .875rem;
          height: .875rem;
        }
      }
    }
    .sancel {
      margin-left: .625rem;
      h2 {
        width: 2rem;
        height: .625rem;
        line-height: .3125rem;
        font-size: 1rem;
        color: #4c84ff;
        font-weight: 700;
      }
    }
  }
  .search_res {
    // 搜索结果
    display: flex;
    justify-content: center;
    padding: .9375rem 0;
    border-bottom: .0625rem solid #edeff0;
    .left {
      width: 5.375rem;
      height: 6.9375rem;
      border-radius: .3125rem;
      box-shadow: 0 .125rem .25rem .125rem #ddd;
      margin-right: .625rem;
      img {
        width: 100%;
        margin-top: .78125rem;
      }
    }
    .right {
      width: 15.5625rem;
      height: 6.9375rem;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      .top {
        margin-bottom: 1.75rem;
        font-size: .9375rem;
        font-weight: 700;
      }
      .botton {
        display: flex;
        justify-content: space-between;
        .botton_left {
          font-size: .75rem;
          padding-top: .125rem;
        }
        .botton_right {
          & > :nth-child(1) {
            font-size: .75rem;
            text-decoration: line-through;
          }
          & > :nth-child(2) {
            font-size: 1rem;
            font-weight: 700;
          }
        }
      }
    }
  }
  //无搜索结果
  .custom-image .van-empty__image {
    margin-top: 6.25rem;
    width: 5.625rem;
    height: 5.625rem;
  }
}
</style>
<script>
export default {
  data() {
    return {
      items: [], // 所有的商品列表
      cart: [], //存符合条件的商品
    };
  },
  //搜索查询的结果
  methods: {
    search(e) {
      let n = e.target.value;
      this.cart = this.items.filter((val) => {
        return val.title.includes(n);
      });
    },
  },
  mounted() {
    // 发送http请求，返回所有书籍数据
    this.axios.get("/second/list").then((result) => {
      // console.log(result.data);
      let items = result.data.data;
      this.items = items;
    });
  },
};
</script>
